package com.quan.server.service;

import com.mark.util.CommonUtil;
import com.quan.server.beans.TaobaoOrder;
import com.quan.server.jpa.TaobaoOrderRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@Component
@Path("/taobaoOrders")
public class TaobaoOrderService {
    private static Logger logger = LoggerFactory.getLogger(TaobaoOrderService.class);
    @Autowired
    private TaobaoOrderRepository taobaoOrderRepository;

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response post(TaobaoOrder taobaoOrder) {
        try {
            taobaoOrder.id = CommonUtil.createRandomId();
            taobaoOrder = taobaoOrderRepository.save(taobaoOrder);
            return Response.status(Response.Status.CREATED).entity(taobaoOrder).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response delete(@PathParam("id") String id) {
        try {
            taobaoOrderRepository.delete(id);
            return Response.status(Response.Status.NO_CONTENT).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public Response put(TaobaoOrder taobaoOrder) {
        try {
            if (CommonUtil.isEmpty(taobaoOrder.id)) {
                taobaoOrder.id = CommonUtil.createRandomId();
            }
            taobaoOrder = taobaoOrderRepository.save(taobaoOrder);
            return Response.ok(taobaoOrder).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response get(@PathParam("id") String id) {
        try {
            TaobaoOrder taobaoOrder = taobaoOrderRepository.findOne(id);
            if (taobaoOrder == null) {
                return Response.status(Response.Status.NO_CONTENT).build();
            } else {
                return Response.ok(taobaoOrder).build();
            }
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAll() {
        try {
            List<TaobaoOrder> taobaoOrderList = taobaoOrderRepository.findAll();
            return Response.ok(taobaoOrderList).build();
        } catch (Exception e) {
            logger.error("", e);
            return Response.serverError().build();
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("branch")
    public Response post(List<TaobaoOrder> taobaoOrderList) {
        for (TaobaoOrder TaobaoOrder : taobaoOrderList) {
            post(TaobaoOrder);
        }
        return Response.ok().build();
    }
}
